import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yp/config/style.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'button_widget.dart';
import 'line.dart';

class payBottomSheet {
  /*
    context:上下文对象
    state:{
    isGiftNum:选择的购物卡数量
    GiftcardList:选择的购物卡数组
    isGiftMoney:计算之后需支付钱数
    realMoney:小于0真实的价格（用户不可见）
    }
    goPay:去支付的方法
       clickGift:选择购物卡的方法
     */
  static void showPay(BuildContext context, state, Function goPay, clickGift, {tmlnumid}) {
    state.GiftcardList.forEach((element) {
      element['click'] = false;
    });
    state.isGiftNum = 0;
    state.isGiftMoney = state.total["all_payment"];
    state.realMoney = state.total["all_payment"];
    showModalBottomSheet(
        context: context,
        // isDismissible: true,
        isScrollControlled: true,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topLeft: Radius.circular(15), topRight: Radius.circular(15))),
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context1, state1) {
            return AnimatedPadding(
                //showModalBottomSheet 键盘弹出时自适应
                padding: MediaQuery.of(context).viewInsets,
                duration: const Duration(milliseconds: 100),
                child: Container(
                    constraints: BoxConstraints(
                      minHeight: 90,
                      maxHeight: MediaQuery.of(context).size.height / 1.2,
                    ),
                    child: ListView(shrinkWrap: true, children: [
                      Container(
                          alignment: Alignment.bottomCenter,
                          padding: EdgeInsets.only(top: 12),
                          child: Row(
                            children: [
                              SizedBox(width: 36),
                              Expanded(
                                child: Text('选择支付方式', style: TextStyle(fontSize: 32.sp, color: Color(0xff666666), fontWeight: FontWeight.normal), textAlign: TextAlign.center),
                              ),
                              EsIconButton(
                                Icon(
                                  Icons.close,
                                  size: 24,
                                  color: Color(0xffcccccc),
                                ),
                                padding: EdgeInsets.only(right: 12),
                                onTap: () {
                                  Navigator.of(context).pop();
                                },
                              )
                            ],
                          )),
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 20),
                        child: Row(
                          children: [
                            // Image.asset(
                            //     'assets/images/selectIcon1.png',
                            //     width: 16,
                            //     height: 16),
                            SizedBox(
                              width: 8,
                            ),
                            Text(
                              '福利卡（已选${state.isGiftNum}张，共${state.GiftcardList.length}张）',
                              style: TextStyle(color: Color(0xff333333), fontSize: 32.sp, fontWeight: FontWeight.normal),
                            ),
                            // Expanded(
                            //     child: Text(
                            //   '￥100.00',
                            //   maxLines: 1,
                            //   overflow:
                            //       TextOverflow
                            //           .ellipsis,
                            //   textAlign:
                            //       TextAlign
                            //           .end,
                            //   style: TextStyle(
                            //       color: EsColors
                            //           .TitleSecondary,
                            //       fontSize:
                            //           EsSizes
                            //               .MD,
                            //       fontWeight:
                            //           EsFontWeights
                            //               .Medium),
                            // ))
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 36, bottom: 18),
                        height: 155,
                        child: ListView.builder(
                            itemCount: state.GiftcardList.length,
                            scrollDirection: Axis.horizontal,
                            shrinkWrap: true,
                            itemBuilder: (context, index) {
                              return GestureDetector(
                                onTap: () {
                                  state1(() {
                                    clickGift(index);
                                  });

                                  print('选择礼品卡');
                                },
                                child: Stack(children: [
                                  Container(
                                    width: 190,
                                    padding: EdgeInsets.all(6),
                                    margin: EdgeInsets.only(right: 10),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: [
                                        Image.asset('assets/images/welfare_card_cover.jpeg', width: 176, height: 100),
                                        // CachedImage(
                                        //     width: 176,
                                        //     height: 100,
                                        //     padding: EdgeInsets.zero,
                                        //     scale: BoxFit.fitWidth,
                                        //     path:
                                        //         'assets/images/welfare_card_cover'),
                                        Expanded(child: SizedBox(height: 2)),
                                        Text(
                                          '福利卡',
                                          style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Normal),
                                        ),
                                        Row(
                                          children: [
                                            Expanded(
                                              child: Text(
                                                '￥${state.GiftcardList[index]['money_available'].toString()}',
                                                maxLines: 1,
                                                overflow: TextOverflow.ellipsis,
                                                style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Medium),
                                              ),
                                            ),
                                            // Text(
                                            //   '有效期至:${ state.GiftcardList[index]['expire_dtme'].toString()}',
                                            //   style: TextStyle(
                                            //       color: EsColors.Fill,
                                            //       fontSize: EsSizes.XXS,
                                            //       fontWeight:
                                            //           EsFontWeights.Normal),
                                            // )
                                          ],
                                        )
                                      ],
                                    ),
                                    decoration: BoxDecoration(
                                        color: EsColors.White,
                                        border: Border.all(
                                          color: EsColors.Primary,
                                          width: 1,
                                        ),
                                        borderRadius: BorderRadius.circular(3)),
                                  ),
                                  if (state.GiftcardList[index]['click']) Positioned(left: 0, top: 0, child: Image.asset('assets/images/selected_left.png', width: 47, height: 47))
                                ]),
                              );
                            }),
                      ),
                      YpLine(),
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          state1(() {
                            // index = 0;
                            print('切换支付方式微信');
                          });
                        },
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 20),
                          child: Row(
                            children: [
                              Image.asset('assets/images/selectIcon1.png', width: 16, height: 16),
                              SizedBox(
                                width: 8,
                              ),
                              Text(
                                '微信支付',
                                style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.MD, fontWeight: EsFontWeights.Normal),
                              ),
                              Expanded(
                                  child: Text(
                                '￥${state.isGiftMoney}',
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                textAlign: TextAlign.end,
                                style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.MD, fontWeight: EsFontWeights.Medium),
                              ))
                            ],
                          ),
                        ),
                      ),
                      YpLine(),
                      // GestureDetector(
                      //   behavior:
                      //       HitTestBehavior
                      //           .opaque,
                      //   onTap: () {
                      //     state1(() {
                      //       // index = 1;
                      //       print(
                      //           '切换支付方式支付宝');
                      //     });
                      //   },
                      //   child: Padding(
                      //     padding:
                      //         const EdgeInsets
                      //                 .symmetric(
                      //             horizontal:
                      //                 12,
                      //             vertical:
                      //                 20),
                      //   ),
                      // ),
                      // YpLine(),
                      SizedBox(height: 27),
                      // Center(
                      //   child: RichText(
                      //     text: TextSpan(
                      //       children: [
                      //         TextSpan(
                      //           text: '应付：',
                      //           style: TextStyle(
                      //               color: EsColors.Primary,
                      //               fontSize: EsSizes.XS,
                      //               fontWeight: EsFontWeights.Normal),
                      //         ),
                      //         TextSpan(
                      //           text: '￥422.08',
                      //           style: TextStyle(
                      //               color: EsColors.Primary,
                      //               fontSize: EsSizes.LG,
                      //               fontWeight: EsFontWeights.Medium),
                      //         )
                      //       ],
                      //     ),
                      //   ),
                      // ),
                      SizedBox(height: 16),
                      EsBottomWidget(
                          leftTitle: null,
                          rightTitle: '确认付款',
                          rightlick: () {
                            print('确认付款');

                            print(goPay);
                            if (tmlnumid != null) {
                              goPay(tmlnumid);
                            } else {
                              goPay();
                            }

                            Navigator.of(context).pop();
                          },
                          decoration: null)
                    ])));
          });
        });
  }
}
